package visualKernel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ConfigXML {
	
	private boolean showDetails = false;
	private boolean showErrorMessages = false;
	private boolean showFilesInEditor = false;
	private boolean notTranslate = false;
	String solverPath = "";
	Document doc;
	final String xmlString = System.getProperty("user.home") + "/configSMT.xml";
	
	
	
	public boolean isShowDetails() {
		return showDetails;
	}

	public void setShowDetails(boolean showDetails) {
		this.showDetails = showDetails;
	}

	public boolean isShowErrorMessages() {
		return showErrorMessages;
	}

	public void setShowErrorMessages(boolean showErrorMessages) {
		this.showErrorMessages = showErrorMessages;
	}

	public boolean isShowFilesInEditor() {
		return showFilesInEditor;
	}

	public void setShowFilesInEditor(boolean showFilesInEditor) {
		this.showFilesInEditor = showFilesInEditor;
	}

	public boolean isNotTranslate() {
		return notTranslate;
	}

	public void setNotTranslate(boolean notTranslate) {
		this.notTranslate = notTranslate;
	}

	public String getSolverPath() {
		return solverPath;
	}

	public void setSolverPath(String solverPath) {
		this.solverPath = solverPath;
	}

	public ConfigXML()
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try
		{
			DocumentBuilder docBuilder = dbf.newDocumentBuilder();
			File xmlFile = new File(xmlString);
			if(!xmlFile.exists())
			{
				try {
					xmlFile.createNewFile();
					updateInfo();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return;
			}
			else
			{
				Document doc = docBuilder.parse(xmlFile);
				Element smtconfig = doc.getDocumentElement();
				if(smtconfig == null)
				{
					updateInfo();
				}
				NodeList els = smtconfig.getElementsByTagName("solverurl");
				Element solverurl = (Element)els.item(0);
				solverPath = solverurl.getTextContent();
				
				els = smtconfig.getElementsByTagName("showdetails");
				solverurl = (Element)els.item(0);
				showDetails = Boolean.valueOf(solverurl.getTextContent());
				
				els = smtconfig.getElementsByTagName("showerror");
				solverurl = (Element)els.item(0);
				showErrorMessages = Boolean.valueOf(solverurl.getTextContent());
				
				els = smtconfig.getElementsByTagName("showfilesineditor");
				solverurl = (Element)els.item(0);
				showFilesInEditor = Boolean.valueOf(solverurl.getTextContent());
				
				els = smtconfig.getElementsByTagName("nottranslate");
				solverurl = (Element)els.item(0);
				notTranslate = Boolean.valueOf(solverurl.getTextContent());			
			}
		}
		catch(Exception e)
		{
			updateInfo();
		}
	}
	
	public void updateInfo()
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder docBuilder = dbf.newDocumentBuilder();
			doc = docBuilder.newDocument();
			
			Element smtconfig = doc.createElement("smtconfig");

			Element solverurl = doc.createElement("solverurl");
			solverurl.setTextContent(solverPath);
			
			Element showdetails = doc.createElement("showdetails");
			showdetails.setTextContent(String.valueOf(showDetails));
			
			Element showerror = doc.createElement("showerror");
			showerror.setTextContent(String.valueOf(showErrorMessages));
			
			Element showFileInEditor = doc.createElement("showfilesineditor");
			showFileInEditor.setTextContent(String.valueOf(showFilesInEditor));
			
			Element notDoTranslate = doc.createElement("nottranslate");
			notDoTranslate.setTextContent(String.valueOf(notTranslate));
			
			smtconfig.appendChild(solverurl);
			smtconfig.appendChild(showdetails);
			smtconfig.appendChild(showerror);
			smtconfig.appendChild(showFileInEditor);
			smtconfig.appendChild(notDoTranslate);
			
			doc.appendChild(smtconfig);	
			
			try {
				Transformer t = TransformerFactory.newInstance().newTransformer();
				try {
					t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(new File(xmlString))));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (TransformerException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (TransformerConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TransformerFactoryConfigurationError e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		} catch (ParserConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}

}
